package com.huangmb.idea.action;

import com.huangmb.idea.bean.BaseTask;
import com.huangmb.idea.bean.TranslateResult;
import com.huangmb.idea.utils.UIHelper;
import com.huangmb.idea.utils.TaskManager;
import com.huangmb.idea.utils.TranslateUtils;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.TextRange;
import org.apache.http.util.TextUtils;

/**
 * Created by huangmb on 2016/12/10.
 */
public class TranslateAction extends AnAction {
    private static final String NAME = "TranslateAction";

    @Override
    public void actionPerformed(final AnActionEvent anActionEvent) {
        final Editor editor = anActionEvent.getData(PlatformDataKeys.EDITOR);
        if (editor == null) {
            return;
        }
        String word = getWordToTranslate(editor);
        System.out.println(word);
        if (TextUtils.isBlank(word)) {
            return;
        }
        TranslateUtils.query(word,
                new TaskManager.CallBack() {
                    @Override
                    public void onSuccess(BaseTask task, TranslateResult result) {
                        String s = result.toString();
                        //UIHelper.showNotification(NAME, s);
                        UIHelper.showPopupBalloon(s, editor);
                    }

                    @Override
                    public void onFailed(BaseTask task, Exception e) {
                        UIHelper.showNotification(NAME, e.toString());
                        UIHelper.showPopupBalloon(e.getMessage(), editor);
                    }
                }
        );


    }

    @Override
    public void update(AnActionEvent e) {
        /*final Editor editor = e.getData(PlatformDataKeys.EDITOR);
        if (editor == null){
            getTemplatePresentation().setEnabled(false);
            return;
        }
        Document document = editor.getDocument();
        CaretModel caretModel = editor.getCaretModel();
        int caretOffset = caretModel.getOffset();

        int lineNum = document.getLineNumber(caretOffset);
        int lineStartOffset = document.getLineStartOffset(lineNum);
        int lineEndOffset = document.getLineEndOffset(lineNum);

        if (lineEndOffset == lineStartOffset){
            getTemplatePresentation().setEnabled(false);
            return;
        }
        //编辑框无焦点时翻译不可用
        getTemplatePresentation().setEnabled(true);*/

    }

    private String getWordToTranslate(Editor editor) {
        SelectionModel model = editor.getSelectionModel();
        String selectedText = model.getSelectedText();
        if (!TextUtils.isBlank(selectedText)) {
            return selectedText;
        }
        Document document = editor.getDocument();
        CaretModel caretModel = editor.getCaretModel();
        int caretOffset = caretModel.getOffset();

        int lineNum = document.getLineNumber(caretOffset);
        int lineStartOffset = document.getLineStartOffset(lineNum);
        int lineEndOffset = document.getLineEndOffset(lineNum);

        if (lineEndOffset == lineStartOffset){
            return null;
        }
        String lineContent = document.getText(new TextRange(lineStartOffset, lineEndOffset));
        char[] chars = lineContent.toCharArray();
        int start = 0, end = 0, cursor = caretOffset - lineStartOffset - 1;
        if (cursor < 0){
            cursor = 0;
        }
        if (!Character.isLetter(chars[cursor])) {
            UIHelper.showPopupBalloon("Caret not in a word", editor);
            return null;
        }

        for (int ptr = cursor; ptr >= 0; ptr--) {
            if (!Character.isLetter(chars[ptr])) {
                start = ptr + 1;
                break;
            }
        }

        int lastLetter = 0;
        for (int ptr = cursor; ptr < lineEndOffset - lineStartOffset; ptr++) {
            lastLetter = ptr;
            if (!Character.isLetter(chars[ptr])) {
                end = ptr;
                break;
            }
        }
        if (end == 0) {
            end = lastLetter + 1;
        }

        return new String(chars, start, end - start);
    }

}
